package geektime.spring.springbucks.mapper;

import geektime.spring.springbucks.model.Coffee;

import java.util.List;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.session.RowBounds;

@Mapper
public interface CoffeeMapper {
    @Select("select * from t_coffee order by id")
    List<Coffee> findAllWithRowBounds(RowBounds rowBounds);

    @Select("select * from t_coffee order by id")
    List<Coffee> findAllWithParam(@Param("pageNum") int pageNum,
                                  @Param("pageSize") int pageSize);

    @Select("select * from t_coffee where name = #{name}")
    Coffee findOneCoffeeByName(@Param("name") String name);

    @Select("<script>" +
            "select * from t_coffee a  " +
            "where a.id in " +
            "<foreach collection = 'id' index = 'index' item = 'item' open = '(' separator = ','  close = ')'>#{item}</foreach> " +
            "</script>")
    List<Coffee> findCoffeeByListID(@Param("id") List<Long> id);

    @Select("insert into t_coffee (name, price, create_time, update_time) values (#{name}, #{price}, now(), null)")
    void addCoffee(@Param("name") String name, @Param("price") Long price);

    @Select("delete from t_coffee where name = #{name}")
    void delCoffeeByName(@Param("name") String name);

    @Select("update t_coffee set name = #{name}, price = #{price} where id = #{id}")
    void updateCoffeeByID(@Param("name") String name, @Param("price") Long price, @Param("id") Long id);

    @Select("SELECT COUNT(*) AS CNT FROM t_coffee")
    long getCount();

}